1


1

Hey guys, Imma teach you how to write a Lua binary module in C/C++ and make it work in CS2D using Dev-C++. Our module should read the files inside a directory and return them in a table.

1st step:
Download the Lua binaries for windows:
Luaforge
We will be downloading lua5.1.4 Win32 bin.zip
Place Lua5.1.dll in your Dev-C++/bin/ folder.


2nd step:
Download the lua source from Lua.org
Extract the following files and place them in your Dev-C++/Include/Lua folder:
Lauxlib.h
lua.h
luaconf.h
lualib.h
And in your Dev/C++/include folder make a file called Lua.hpp that should contain this:
Code


3rd step:
Create a new windows Dll project
Screenshot


4th step:
Here you should write your code after all the stuff declared after the template in our dllmain.cpp file
Code


5th step:
We make a function that should be registering our C++ functions
Code


6th step:
We surround both our init and myluareadfolder functions with an extern "C" construction so that our compiller knows that these are C funcions , just like in Lua.hpp Since Lua is completely written in C we will have to do this for C++ so that our compiler know that these are C functions.


Now you can compile.
Congratulations, you have successfully made a Lua binary extension without severely hurting/injuring/killling anybody


7th step:
Copy the lua5.1.dll file from your Dev-C++/bin directory and paste in a CS2D/libs/ folder, or paste it in C:/Windows/System32/


8th step:
Now that we have everything ready we will call our library through Lua. Code And voila, we can call our C++ function using read_dir() and passing the folder you want to read


Notes:
I didn't explain too much because this would have been a very large post but you can ask something and I will (hopefully) answer ^^

flag offensive
asked 2010-04-27 22:48:59.911779
21
comments (1)
1 Answers:
0


Haha pretty fucking awesome :D

I managed to create my own sethealth function in c :D This way, you can go beyond the standard 250 HP / 100 armor


function loadbinaries()
local f,e = package.loadlib("sys/my_lua/Lua_ext.dll","init")
if e then print(e) return nil end
f()
msg("Loading library succeeded!")

end

loadbinaries()

a = my_function(1,2,3,"abc","def")

msg(string.format("my_function() returned %i",a))

function set(am) h = make_god(am) end

addhook("say","mysay") function mysay(id,txt) if(txt == "!god") then set(5000) msg("health : "..player(1,"health")) end end


and the c code:

int make_god(lua_State *L)
{
    int argc = lua_gettop(L);
    int health = lua_tointeger(L,argc);

    __int32 * pStruct = (__int32 *)0x61D42C;
    CPlayer * local = (CPlayer *)(*pStruct);

    local->health = health;
    local->armor = health;

    return health;      
}
permanent link | flag offensive
answered 2010-05-02 12:51:25.464659
46
add comment
Your answer:
You are now not logged in but you can answer first and then login
toggle preview



Tags:

× 3
× 1
× 1
× 1

Asked: 2 years ago

Seen: 2,146 times

Last updated: 2 years ago

Made with Django.